<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use App\Models\User;
use App\Models\Brand;
use App\Models\BrandModel;
use App\Models\HwCity;
use Brick\Math\RoundingMode;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;


class Init extends Command
{
    protected $signature = 'cc:init';

    protected $description = "Init everyday data from zero";

    public function handle()
    {
	   $brands  = Brand::all();
	   BrandModel::truncate();
	   foreach($brands as $brand){

	   		  $response = Http::get('https://m.58moto.com/clientApi/carport/goods/v4/brand/'.$brand->id.'?platform=11&version=3.59.0&page=0&rows=1000');
	   		   if ($response->successful()) {
	   		   		$data = $response->json();
	   		   		 $insert = [];
		             foreach($data['data'] as $k=>$v){

		                $logo = $v['goodPic'];
		                $imageContent = file_get_contents($logo);

		                $newFileName = "motuo/".Str::random(40) . '.jpg'; // 假设是JPG格式

		                Storage::disk('oss')->put($newFileName, $imageContent);
		                $insert[$k] = [

		                    'id'=>$v['goodId'],
		                    'brand_id'=>$brand->id,
		                    'name' => $v['goodName'],
		                    'image'=>$newFileName,
		                    'created_at'=>Carbon::now(),
		                ];

		               

		            }

		             BrandModel::insert($insert);

		             echo $brand->id;

	   		   }

	   }

	   /*$brands  = BrandModel::all();
	   foreach($brands as $brand){

	   		if(strstr($brand->image,'https://littleshop.oss-cn-beijing.aliyuncs.com/')){
	   			
	   			$newFileName = str_replace("https://littleshop.oss-cn-beijing.aliyuncs.com/",'',$brand->image);

	   		}else{

	   			$logo = "https://littleshop.oss-cn-beijing.aliyuncs.com".$brand->image;
	            $imageContent = file_get_contents($logo);

	            $newFileName = "motuo/".Str::random(40) . '.jpg'; // 假设是JPG格式

	            Storage::disk('public')->put($newFileName, $imageContent);
		   		}

	   		


	   		$brand->update(['image'=>$newFileName]);

	   		echo $brand->id;

	   }*/






	   /*$brands  = Brand::query()->where('id',368)->get();
	   //BrandModel::truncate();
	   foreach($brands as $brand){

	   		  $response = Http::get('https://m.58moto.com/clientApi/carport/goods/v4/brand/'.$brand->id.'?platform=11&version=3.59.0&page=0&rows=1000');
	   		   if ($response->successful()) {
	   		   		$data = $response->json();
	   		   		 $insert = [];
		             foreach($data['data'] as $k=>$v){

		             	
		             		$logo = $v['goodPic'];
			                $imageContent = file_get_contents($logo);

			                $newFileName = "motuo/".Str::random(40) . '.jpg'; // 假设是JPG格式

			                Storage::disk('public')->put($newFileName, $imageContent);

			                BrandModel::find($v['goodId'])->update(['image'=>$newFileName]);

		             	
		                

		               

		            }

		            // BrandModel::insert($insert);

		             echo $brand->id;

	   		   }

	   }*/
    }
}
